package com.jx.flowershopmanager.home.fragment;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.jx.flowershopmanager.App;
import com.jx.flowershopmanager.R;
import com.jx.flowershopmanager.adapter.MyGdAdapter;
import com.jx.flowershopmanager.adapter.OrderAdapter;
import com.jx.flowershopmanager.base.BaseFragment;
import com.jx.flowershopmanager.entity.FlowersEntity;
import com.jx.flowershopmanager.entity.SalesRecordEntity;
import com.jx.flowershopmanager.home.order.AddEditOrderActivity;
import com.jx.flowershopmanager.model.Flowers;
import com.jx.flowershopmanager.model.SalesRecord;
import com.jx.flowershopmanager.network.RetrofitCallback;
import com.jx.flowershopmanager.network.RetrofitUtil;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class OrderFragment extends BaseFragment {

    /**
     * 适配器
     */
    private OrderAdapter orderAdapter;
    /**
     * 列表
     */
    private RecyclerView recyclerView;

    /**
     * 数据集合
     */
    private List<SalesRecord.Data> salesRecordList = new ArrayList<>();
    private ImageView addImage;
    @Override
    protected int layoutId() {
        return R.layout.fragment_order;
    }

    @Override
    protected void initView(View view) {
        recyclerView = view.findViewById(R.id.order_listview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(requireContext());
        recyclerView.setLayoutManager(layoutManager);
        addImage = view.findViewById(R.id.add_img);
        addImage.setOnClickListener(v->{
            startActivity(new Intent(requireContext(), AddEditOrderActivity.class));
        });
    }

    @Override
    protected void initData(Context context) {
        querySalesRecord();
    }

    private void querySalesRecord(){
        Map<String, String> map = new HashMap<>(16);
        RetrofitUtil.get("v1/getSalesRecord", map, new RetrofitCallback() {
            @Override
            public void onSuccess(String resultJsonString) {
                Gson gson = new Gson();
                SalesRecord salesRecord = gson.fromJson(resultJsonString,SalesRecord.class);
                if (salesRecord.success){
                    salesRecordList = salesRecord.getData();
                    orderAdapter = new OrderAdapter(salesRecordList);
                    recyclerView.setAdapter(orderAdapter);
                    orderAdapter.notifyDataSetChanged();
                    Log.i("OrderFragment",String.valueOf(salesRecord.getData().size()));
                }
            }

            @Override
            public void onError(Throwable t) {
                Log.i("OrderFragment", t.getMessage());

            }
        });
    }

    /*
     * 初始化订单数据
     */

//    @RequiresApi(api = Build.VERSION_CODES.O)
//    @SuppressLint("Range")
//    private void initOrder(){
//        SQLiteDatabase db = App.getDbHelp().getWritableDatabase();
//        Cursor cursor = db.query("SalesRecord", null, null, null, null, null, null);
//        List<SalesRecordEntity> salesRecordEntities = new ArrayList<>();
//        if (cursor.moveToFirst()) {
//            do {
//                Integer id = cursor.getInt(cursor.getColumnIndex("id"));
//                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH);
//                LocalDate localDate = LocalDate.parse(cursor.getString(cursor.getColumnIndex("date")), formatter);
//                java.sql.Date date = convertToDateViaSqlDate(localDate);
//                Integer count = cursor.getInt(cursor.getColumnIndex("count"));
//                double total = cursor.getDouble(cursor.getColumnIndex("total"));
//                String client = cursor.getString(cursor.getColumnIndex("client"));
//                String remark = cursor.getString(cursor.getColumnIndex("remark"));
//                String emp = cursor.getString(cursor.getColumnIndex("emp"));
//                LocalDate localCrateDate = LocalDate.parse(cursor.getString(cursor.getColumnIndex("createAt")), formatter);
//                java.sql.Date createAt = convertToDateViaSqlDate(localCrateDate);
//                LocalDate localupdateAt = LocalDate.parse(cursor.getString(cursor.getColumnIndex("updateAt")), formatter);
//                java.sql.Date updateAt = convertToDateViaSqlDate(localupdateAt);
//                salesRecordEntities.add(new SalesRecordEntity(id,date,count,total,client,remark,emp,createAt,updateAt));
//            } while (cursor.moveToNext());
//        }
//        Log.i("TAG",String.valueOf(salesRecordEntities.size()));
//        if (salesRecordEntities.size()<=0){
//            imageView.setVisibility(View.VISIBLE);
//            recyclerView.setVisibility(View.GONE);
//        }
//        orderAdapter = new OrderAdapter(salesRecordEntities);
//        recyclerView.setAdapter(orderAdapter);
//        orderAdapter.notifyDataSetChanged();
//        cursor.close();
//    }

    public java.sql.Date convertToDateViaSqlDate(LocalDate dateToConvert) {
        return java.sql.Date.valueOf(String.valueOf(dateToConvert));
    }
}